import {
  AreaChartOutlined,
  EllipsisOutlined,
  PlusOutlined,
} from '@ant-design/icons';
import React, { useEffect, useState } from 'react';
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { ProTable, TableDropdown } from '@ant-design/pro-components';
import {
  Button,
  Col,
  Dropdown,
  Form,
  Input,
  Modal,
  Row,
  Switch,
  Tooltip,
} from 'antd';
import { useRef } from 'react';
import { request, history, connect, useSelector } from 'umi';
import { getTime } from '@/utils/time';
import { useAddKey } from '@/pages/hooks/index';

// import PubSub from 'pubsub-js';
type GithubIssueItem = {
  url: string;
  id: number;
  number: number;
  title: string;
  labels: {
    name: string;
    color: string;
  }[];
  state: string;
  comments: number;
  created_at: string;
  updated_at: string;
  closed_at?: string;
};

const style: React.CSSProperties = { background: '#0092ff' };
const styles: React.CSSProperties = { background: 'red' };
const sty: React.CSSProperties = { color: 'white' };
interface DataType {
  key: React.Key;
  name: string;
  age: number;
  address: string;
}

function Table(props: any) {
  console.log(props);

  const actionRef = useRef<ActionType>();

  const [selectionType, setSelectionType] = useState<'checkbox' | 'radio'>(
    'checkbox',
  );
  // rowSelection object indicates the need for row selection
  const rowSelection: any = {
    onChange: (selectedRowKeys: React.Key[], selectedRows: DataType[]) => {
      console.log(
        `selectedRowKeys: ${selectedRowKeys}`,
        'selectedRows: ',
        selectedRows,
      );
    },
    getCheckboxProps: (record: DataType) => ({
      disabled: record.name === 'Disabled User', // Column configuration not to be checked
      name: record.name,
    }),
  };
  // 编辑
  const goEdit = (id: any) => {
    console.log(id);
    history.push('/home/shop/build/add', { flag: 4,id,tit:"编辑" });
  };
  // 删除
  const del = (id: any) => {
    // props.fn(id)
    props.dispatch({ type: 'shop/DEL', id });
  };

  const goDetail = () => {
    history.push('/home/shop/build/add', { flag: 4,tit:"添加" });
  };

  const columns: ProColumns<GithubIssueItem>[] = [
    {
      title: 'ID',
      dataIndex: 'id',
      key: 'id',
      ellipsis: true,
    },
    {
      title: '楼盘名称',
      dataIndex: 'estate_name',
      key: 'estate_name',
    },
    {
      title: '标题',
      dataIndex: 'title',
      // ellipsis: true,
      key: 'title',
      render: (item, record: any) => {
        const img =
          record.rec_position === 1 ? (
            <img
              loading="lazy"
              src="http://fangadmin.shbwyz.com/static/images/rec.png"
              alt=""
            />
          ) : (
            ''
          );
        return (
          <React.Fragment>
            {item}
            {img}
            {record.img ? (
              <Tooltip
                title={
                  <img
                    className="ll-img"
                    src={`http://fangadmin.shbwyz.com${record.img}`}
                  ></img>
                }
              >
                <AreaChartOutlined />
              </Tooltip>
            ) : (
              ''
            )}
          </React.Fragment>
        );
      },
    },
    {
      disable: true,
      title: '面积',
      key: 'acreage',
      dataIndex: 'acreage',
      render: (item: any) => {
        return `${item}/m²`;
      },
    },
    {
      title: '总价',
      key: 'price',
      dataIndex: 'price',
      render: (item) => {
        return `${item}万`;
      },
    },

    {
      disable: true,
      title: '排序',
      dataIndex: 'ordid',
      key: 'ordid',
    },
    {
      disable: true,
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      render: (item) => {
        return (
          <Switch
            checkedChildren="正常"
            unCheckedChildren="待审"
            defaultChecked={item === 0 ? false : true}
          />
        );
      },
    },
    {
      title: '操作',
      valueType: 'option',
      key: 'option',
      render: (text, record, _, action) => {
        return (
          <div className="aka">
            <Button type="primary" onClick={() => goEdit(record.id)}>
              编辑
            </Button>
            <Button type="danger" onClick={() => del(record.id)}>
              删除
            </Button>
          </div>
        );
      },
    },
  ];
  return (
    <div>
      <ProTable<GithubIssueItem>
        columns={columns}
        rowSelection={{
          type: selectionType,
          ...rowSelection,
        }}
        actionRef={actionRef}
        cardBordered
        dataSource={useAddKey(props.data)}
        editable={{
          type: 'multiple',
        }}
        columnsState={{
          persistenceKey: 'pro-table-singe-demos',
          persistenceType: 'localStorage',
          onChange(value) {
            // console.log('value: ', value);
          },
        }}
        rowKey="id"
        search={false}
        options={{
          setting: {
            listsHeight: 400,
          },
        }}
        form={{
          // 由于配置了 transform，提交的参与与定义的不同这里需要转化一下
          syncToUrl: (values, type) => {
            if (type === 'get') {
              return {
                ...values,
                created_at: [values.startTime, values.endTime],
              };
            }
            return values;
          },
        }}
        pagination={{
          pageSize: 5,
          onChange: (page) => console.log(page),
        }}
        dateFormatter="string"
        headerTitle="高级表格"
        toolBarRender={() => [
          <Button
            // onClick={addTable}
            key="button"
            icon={<PlusOutlined />}
            type="primary"
            onClick={goDetail}
          >
            新建
          </Button>,
          <Dropdown
            key="menu"
            menu={{
              items: [
                {
                  label: '1st item',
                  key: '1',
                },
                {
                  label: '2nd item',
                  key: '1',
                },
                {
                  label: '3rd item',
                  key: '1',
                },
              ],
            }}
          >
            <Button>
              <EllipsisOutlined />
            </Button>
          </Dropdown>,
        ]}
      />
    </div>
  );
}
const mapStateToProps = (state: any) => {
  console.log(state);

  return {};
};
export default connect(mapStateToProps)(Table);
